<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Model\Qa;
use App\Model\Setting\SettingOfficialChat;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Content;

class OfficialQuestionController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     * @return Content
     */

    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('小助手回复消息');
            $content->description('');
            $content->body($this->grid());
        });
    }

    /**
     * 编辑
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('编辑');
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('添加');
            $content->description('');
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(SettingOfficialChat::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            // $grid->id('问题ID')->sortable();
            $grid->key_word('关键词')->editable();
            $grid->offcial_answer('官方回答')->editable();
            $grid->flag('状态')->display(function ($data) {
                return $data == 1 ? '启用' : '禁用';
            });
            //禁用导出
            $grid->disableExport();
            //禁用行选择
            $grid->disableRowSelector();
            //$grid->disableActions();
            $grid->filter(function ($filter) {
                // $filter->like('question', '问题');
                // $filter->like('answer', '答案');
                $filter->equal('flag', '使用状态')->select([-1 => '全部', 0 => '禁用', 1 => '启用']);
            });
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableDelete();
                $actions->disableView();
            });
        });
    }

    /**
     * Make a form builder.
     * @return Form
     */
    protected function form($mode = '', $id = '')
    {
        return Admin::form(SettingOfficialChat::class, function (Form $form) use ($mode, $id) {
            $form->text('key_word', '关键词');
            $form->textarea('offcial_answer', '答案');
            $form->select('flag', '是否开启')->options([0 => '禁用', 1 => '启用'])->default(1);
        });
    }
}
